<template>
  <div class="the-page" v-if="pageDetail">

    <div class="panel-search">
      <el-form ref="form">
        <el-form-item label="页面编码：">
          <el-tag type="info">{{pageDetail.id}}</el-tag>
        </el-form-item>
        <el-form-item label="页面名称：">
          <el-tag type="info">{{pageDetail.name}}</el-tag>
        </el-form-item>
        <el-form-item label="页面别名：">
          <el-tag type="info">{{pageDetail.alias}}</el-tag>
        </el-form-item>
      </el-form>

    </div>

    <div class="panel-table">
      <div class="table-head">
        <span>区块管理</span>
        <div>
          <el-button type="primary" icon="icon-plus" @click.stop="createBlockHandle()">新增区块</el-button>
        </div>
      </div>
      <el-table :data="list" stripe :header-cell-style="{backgroundColor:'#ededed',padding:'12px 0'}" style="width: 100%;" border>

        <!-- <el-table :data="list" border fit highlight-current-row style="width: 100%"> -->

        <el-table-column align="center" label="编号" min-width="120" prop="id" />

        <el-table-column align="center" label="页面名称" min-width="120" prop="name">
          <template #default="{row}">
            <!-- <router-link :to="`/config/items/${row.id}` "
                       class="link-type">
            <span>{{ row.name }}</span>
          </router-link> -->
            <p style="cursor:pointer;color:#337ab7;" @click="manageBlockHandle(row)">{{ row.name }}</p>
          </template>
        </el-table-column>

        <el-table-column align="center" label="key" min-width="120" prop="keyName" />

        <el-table-column align="center" label="是否为列表" min-width="120" prop="isList">
          <template #default="{row}">
            <el-tag :type="row.isList?'':'danger'">{{row.isList?'是':'否'}}</el-tag>
          </template>
        </el-table-column>

        <el-table-column align="center" label="最近操作时间" min-width="160" prop="updatedAt" />

        <el-table-column align="center" label="最近操作人" min-width="140" prop="operateName" />

        <el-table-column label="操作" fixed="right" align="center" width="160px">
          <template #default="{row}">
            <el-button @click="manageBlockHandle(row)" type="primary" size="small">内容管理</el-button>
            <el-button @click="deleteBlockHandle(row)" type="danger" plain size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="table-pagination">
        <pagination :total="total" v-model:page="listQuery.page" v-model:limit="listQuery.pageSize" @pagination="getList" />
      </div>

    </div>

    <el-dialog title="新增区块" v-model="blockMadalShow">
      <el-form ref="blockForm" :model="blockForm" :rules="blockFormRules" label-width="100px">
        <el-form-item label="区块名称" prop="name">
          <el-input v-model="blockForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="区块key" prop="keyName">
          <el-input v-model="blockForm.keyName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据类型" prop="classId">
          <el-select v-model="blockForm.classId" placeholder="请选择">
            <el-option v-for="item in classList" :key="item.id" :label="item.title" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否列表" prop="isList" required>
          <el-switch v-model="blockForm.isList" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="blockMadalShow = false">取 消</el-button>
          <el-button type="primary" @click="confirmHandle()">确 定</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import { ElNotification, ElMessageBox } from "element-plus";
import {
  getClassList,
  getPageOne,
  getBlockList,
  createBlock,
  deleteBlock,
} from "@/api/config";
export default {
  name: "universalBlocks",
  components: { Pagination },
  data() {
    return {
      pageDetail: null,
      classList: [],

      list: [],
      listQuery: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      blockMadalShow: false,
      blockForm: {
        name: "",
        keyName: "",
        classId: "",
        isList: 0,
        pageId: this.$route.params && Number(this.$route.params.id),
      },
      blockFormRules: {
        name: [{ required: true, message: "请输入区块名称", trigger: "blur" }],
        keyName: [
          { required: true, message: "请输入区块key", trigger: "blur" },
        ],
        classId: [
          { required: true, message: "请选择数据类型", trigger: "change" },
        ],
      },
      currentPageId: null,
    };
  },
  created() {
    this.getPageDetail();
    this.getList();
    this.getClass();
  },
  methods: {
    getPageDetail(theId) {
      const id = theId || this.$route.params?.id;
      if(id === undefined)return;
      getPageOne(id).then((res) => {
        this.pageDetail = res.data;
      });
    },
    getList(theId) {
      const id = theId || this.$route.params?.id;
      if(id === undefined)return;
      getBlockList(id, this.listQuery).then((res) => {
        this.list = res.data.items;
        this.total = res.data.pagination.total_record;
      });
    },
    getClass() {
      getClassList().then((res) => {
        this.classList = res.data;
      });
    },
    createBlockHandle() {
      this.blockMadalShow = true;
      this.$nextTick(() => {
        this.$refs["blockForm"].resetFields();
        this.blockForm.id = null;
      });
    },
    confirmHandle() {
      this.$refs["blockForm"].validate((valid) => {
        if (valid) {
          createBlock(this.blockForm).then(() => {
            this.blockMadalShow = false;
            this.getList();
          });
        }
      });
    },
    manageBlockHandle(row) {
      // if (row.keyName === "operationPositionGoodsGroup") {
      //   this.$router.push(`/config/otherItems/${row.id}?page=operationItems`);
      // } else if (row.keyName === "kanbanContent") {
      //   this.$router.push(`/config/otherItems/${row.id}?&page=dataViewItems`);
      //   // this.$router.push(`/config/otherItems${row.id}`)
      // } else if (row.keyName === "userHobbySet") {
      //   this.$router.push(`/config/otherItems/${row.id}?&page=userHobbyItems`);
      //   // this.$router.push(`/config/otherItems${row.id}`)
      // } else {
      this.$router.push(`/config/list/items/${row.id}`);
      // }
    },
    deleteBlockHandle(item) {
      ElMessageBox.confirm("是否删除该页面配置?", "提示", {
        type: "warning",
      }).then(() => {
        deleteBlock(item.id).then(() => {
          ElNotification({
            title: "提示",
            message: "删除成功",
            type: "success",
            duration: 2000,
          });
          this.getList();
        });
      });
    },
  },
  beforeRouteUpdate(to,from,next){
    console.log("to,from:",to,from)
    // 从相同路由进入（不同参数），如/a/b/1 => /a/b/2
    this.getPageDetail(to.params.id);
    this.getList(to.params.id)
    next();
  }
};
</script>

<style scope lang="scss">
@import "@/styles/tablePage.scss";
.panel-search{
  padding: 12px 12px 4px 12px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>